﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.ActasDeAnulacion;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;



namespace MS.InformationManagement.WP.DocumentCenter.ActasDeAnulacion
{
    public partial class ActasDeAnulacionUserControl : BaseControl, IActasDeAnulacionView
    {

        #region fields

        private ActasDeAnulacionPresenter _presenter;

        #endregion

        #region Properties

        public string Id { get; set; }
        public List<Radicado> radicadosAnulados { get; set; }
        public bool result { get; set; }
        public string Observacion { get; set; }

        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }

        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new ActasDeAnulacionPresenter(this);
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            //pgrTemplates.NextLinkText = ">>";
            //pgrTemplates.PreviousLinkText = "<<";
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
            pgrTemplates.BuildPager();
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            PageIndex = 0;
            _presenter.HandleLoad();
            pgrTemplates.BuildPager();
        }
        protected void gvAnulados_RowCreated(object sender, GridViewRowEventArgs e)
        {
        }
        public static string getSeccionsById(string IdSeccion)
        {
            string result = string.Empty;
            SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        SPList listSecciones = currentWeb.Lists.TryGetList(GlobalConstants.LIST_BIBLIOTECA_SECCION);
                        if (listSecciones != null)
                        {
                            SPListItem item = listSecciones.GetItemById(int.Parse(IdSeccion));
                            if (item != null)
                            {
                                result = item.Title;
                            }
                        }
                    }
                }
            });
            return result;
        }
        protected void gvAnulados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string seccion = string.Empty;
            e.Row.Cells[0].Visible = false;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (ListHelper.TryToParse(e.Row.Cells[2].Text) != 0)
                    seccion = getSeccionsById(e.Row.Cells[2].Text);

                e.Row.Cells[2].Text = seccion;
            }
        }




        protected void Action_Generar(object sender, EventArgs e)
        {
            if (gvAnulados.Rows.Count > 0)
            {
                _presenter.GenerarActas();
            }
            else
            {
                this._ShowAlertAndClose("No existen registros de radicados anulados");
            }
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            _presenter.HandleLoad();
            pgrTemplates.BuildPager();
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvAnulados.DataSource = radicadosAnulados;
            gvAnulados.DataBind();

            if (radicadosAnulados.Count > 0)
                btnGenerar.Visible = true;
        }
        public void Result()
        {
            if (result == true)
            {
                this._ShowAlertAndClose("Su solicitud ha sido registrada");
                gvAnulados.DataSource = null;
                gvAnulados.DataBind();
                btnGenerar.Visible = false;
            }
            else
            {
                this._ShowAlertAndClose("Ha ocurrido un error al ingresar su solicitud");
            }
        }

        #endregion

 
    }
}
